package com.soc.erp.role;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;

import org.nature.framework.annotation.Ask;
import org.nature.framework.annotation.Ctrl;
import org.nature.framework.annotation.Inject;
import org.nature.framework.annotation.Redirect;
import org.nature.framework.core.NatureCtrl;
import org.nature.framework.core.NatureMap;
import org.nature.framework.util.CastUtil;
import org.nature.framework.util.StringUtil;

import com.soc.erp.employee.Employee;
import com.soc.erp.index.IService;

 



@SuppressWarnings("serial")
@Ctrl(namespace="/role")
public class RoleCtrl extends NatureCtrl{
	public Role role = new Role();
	//public List<NatureMap> permslist = new ArrayList<NatureMap>();
	public String permids ;
	public Set<String> rolePerms;
	public String ask;
	
	public RolePerm role_perm = new RolePerm();
	
	@Inject
	IService iService;
 
	
	@Ask
	public Object list(){
		String sql = "select * from tb_role where 1=1";
		if (StringUtil.isNotBank(role.getString(Role.role_name))) {
			sql += " and role_name like'"+role.getString(Role.role_name)+"'";
		}
		sql += " order by role_id";
		page = iService.page(page, sql);
		iService.jsPage(page, "jsPage");
		return "list.ftl";
	}

	
	@Ask
	public Object oper(){
		if (StringUtil.isNotBank(role.getString(Role.role_id))) {
			role = iService.byId(role);
			ask = "update";
		}else {
			ask = "insert";
		}
		return "oper.ftl";
	}
	
	@Ask
	@Redirect
	public Object update(){
		iService.update(role);
		//operRecordService.insertOper("执行更新角色操作");
		return "list";
	}
	
	@Ask
	@Redirect
	public Object insert(){
		iService.insert(role);
		//operRecordService.insertOper("执行新增角色操作");
		return "list";
	}
	
	@Ask
	@Redirect
	public Object delete(){
		iService.delete(role);
		//operRecordService.insertOper("执行删除角色操作");
		return "list";
	}
	
	 
	
	
	/****/
	@SuppressWarnings("unchecked")
	@Ask
	public Object toperm() {
		String sql = "";
		sql += "SELECT pn.*, pm.module_name, rp.role_id ";
		sql += "FROM tb_perm_name pn ";
		sql += "LEFT JOIN tb_perm_module pm ON pn.module_id=pm.module_id ";
		sql += String.format("LEFT JOIN (SELECT perm_id,role_id FROM tb_role_perm WHERE role_id='%s') rp ON pn.perm_id=rp.perm_id ", role.getString(Role.role_id));
		sql += "ORDER BY pm.module_id, pn.perm_id";
		
		//System.out.println("sql="+sql);
		List<NatureMap> perms = iService.list(sql);
		page.put("perms", perms);
		
		//System.out.println("perms="+perms);
		
		@SuppressWarnings("rawtypes")
		LinkedHashMap permsManager = new LinkedHashMap();
		for (NatureMap perm : perms) {
			String key = perm.getString("module_name");
			if (!permsManager.containsKey(key))
				permsManager.put(key, new ArrayList<NatureMap>());
			((List<NatureMap>) permsManager.get(key)).add(perm);
		}
		page.put("permsManager", permsManager);
		//System.out.println("permsManager="+permsManager);
		return "perm.ftl";
		
	}
	
	@Ask
	public Object update_perm() {
		//System.out.println("perms="+role_perm.getString("perm_id"));
		
		if (StringUtil.isNotBank(role_perm.getString(RolePerm.role_id))) {
			String[] perm_ids = role_perm.getString(RolePerm.perm_id).split(",");
			List<NatureMap> perms = iService.list("SELECT * FROM tb_role_perm WHERE role_id='" + role_perm.getString(RolePerm.role_id)+"'");
			// 删除全部，除了要增加的
			for (NatureMap perm : perms) {
				if (!Arrays.asList(perm_ids).contains(perm.getString("perm_id"))) {
					deleteRoleRight(perm.getString("id"));
					
				}
			}
			
			for (String perm_id : perm_ids) {
				if (StringUtil.isNotBank(perm_id)) {
					//insertRoleRight(role_perm.getInt("nRoleID"), Integer.valueOf(perm_id));
					insertRoleRight(role_perm.getString("role_id"), perm_id);
					
				}
			}
			
			//更新权限
			List<String> opeList = new ArrayList<String>();
			String role_id = (String) getSession().getAttribute("permit");
			String sql = String.format("SELECT perm_id FROM tb_role_perm WHERE role_id='%s'", role_id);
			
			System.out.println(sql);
			List<NatureMap> ops = iService.list(sql);
			for (NatureMap op : ops) {
				opeList.add(op.getString("perm_id")); //更新之后需要重新登录才生效
			}
			getSession().setAttribute("opeList", opeList);	
		}
		return "list";
	}
	
	
	private void insertRoleRight(String role_id, String perm_id) {
		String sql = String.format("SELECT COUNT(*) FROM tb_role_perm WHERE role_id='%s' AND perm_id='%s'", role_id, perm_id);
		Object ct = iService.uniqueValue(sql);
		if (CastUtil.castInt(ct) == 0) {
			RolePerm rolePerm = new RolePerm();
			rolePerm.put(RolePerm.role_id, role_id);
			rolePerm.put(RolePerm.perm_id, perm_id);
			iService.insert(rolePerm);
		}
	}

	private void deleteRoleRight(String id) {
		RolePerm rolePerm = new RolePerm();
		rolePerm.put(RolePerm.id, id);
		iService.delete(rolePerm);
	}
	/****/
	
}
